package android.slc.base.repository.remote;

import android.slc.base.api.slc.callback.po.SlcEntity;
import android.slc.adapter.PageModel;
import android.slc.adapter.SelectBox;
import io.reactivex.rxjava3.functions.Function;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/20 上午 11:22
 */
public class SelectTransformUtils {

    /**
     * Page item转Page selectBox Item
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<PageModel<T>>, SlcEntity<PageModel<SelectBox<T>>>> transitionSelectBoxPageMap() {
        return transitionSelectBoxPageMap(null);
    }

    /**
     * Page item转Page selectBox Item
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<PageModel<T>>, SlcEntity<PageModel<SelectBox<T>>>> transitionSelectBoxPageMap(boolean select) {
        return transitionSelectBoxPageMap(item -> select);
    }

    /**
     * Page item转Page selectBox Item
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<PageModel<T>>, SlcEntity<PageModel<SelectBox<T>>>> transitionSelectBoxPageMap(TransitionSelectBoxDisturb<T> transitionSelectBoxDisturb) {
        return new Function<SlcEntity<PageModel<T>>, SlcEntity<PageModel<SelectBox<T>>>>() {
            @Override
            public SlcEntity<PageModel<SelectBox<T>>> apply(@NotNull SlcEntity<PageModel<T>> pageSlcEntity) {
                PageModel<T> pageModel = pageSlcEntity.getData();
                if (pageSlcEntity.isSuccess() && pageModel == null) {
                    pageModel = new PageModel<>();
                    pageSlcEntity.setData(pageModel);
                }
                SlcEntity<PageModel<SelectBox<T>>> pageSelectBoxSlcEntity = new SlcEntity<>();
                pageSelectBoxSlcEntity.setStatus(pageSlcEntity.getStatus());
                pageSelectBoxSlcEntity.setMsg(pageSlcEntity.getMsg());
                PageModel<SelectBox<T>> selectBoxPageModel = PageTransformUtils.pageModel2TargetRPageModel(pageModel);
                selectBoxPageModel.setRecords(itemTransitionSelectBox(pageModel.getListNoNull(), transitionSelectBoxDisturb));
                pageSelectBoxSlcEntity.setData(selectBoxPageModel);
                return pageSelectBoxSlcEntity;
            }
        };
    }

    /**
     * list item转list selectBox Item
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<List<T>>, SlcEntity<List<SelectBox<T>>>> transitionSelectBoxListMap() {
        return transitionSelectBoxListMap(null);
    }

    /**
     * list item转list selectBox Item
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<List<T>>, SlcEntity<List<SelectBox<T>>>> transitionSelectBoxListMap(boolean select) {
        return transitionSelectBoxListMap(item -> select);
    }

    /**
     * list item转list selectBox Item
     *
     * @param <T>
     * @return
     */
    public static <T> Function<SlcEntity<List<T>>, SlcEntity<List<SelectBox<T>>>> transitionSelectBoxListMap(TransitionSelectBoxDisturb<T> transitionSelectBoxDisturb) {
        return new Function<SlcEntity<List<T>>, SlcEntity<List<SelectBox<T>>>>() {
            @Override
            public SlcEntity<List<SelectBox<T>>> apply(@NotNull SlcEntity<List<T>> listSlcEntity) {
                List<T> dataList = listSlcEntity.getData();
                if (listSlcEntity.isSuccess() && dataList == null) {
                    dataList = new ArrayList<>();
                    listSlcEntity.setData(dataList);
                }
                SlcEntity<List<SelectBox<T>>> listSelectBoxSlcEntity = new SlcEntity<>();
                listSelectBoxSlcEntity.setStatus(listSlcEntity.getStatus());
                listSelectBoxSlcEntity.setMsg(listSlcEntity.getMsg());
                listSelectBoxSlcEntity.setData(itemTransitionSelectBox(dataList, transitionSelectBoxDisturb));
                return listSelectBoxSlcEntity;
            }
        };
    }


    /**
     * list item直接转list selectBox Item
     *
     * @param dataList
     * @param select
     * @param <T>
     * @return
     */
    public static <T> List<SelectBox<T>> itemTransitionSelectBox(List<T> dataList, boolean select) {
        return itemTransitionSelectBox(dataList, item -> select);
    }

    /**
     * list item直接转list selectBox Item
     *
     * @param dataList
     * @param transitionSelectBoxDisturb
     * @param <T>
     * @return
     */
    public static <T> List<SelectBox<T>> itemTransitionSelectBox(List<T> dataList, TransitionSelectBoxDisturb<T> transitionSelectBoxDisturb) {
        List<SelectBox<T>> selectBoxList = new ArrayList<>();
        CollectionUtils.forAllDo(dataList, (item) -> {
            SelectBox<T> selectBox = new SelectBox<>(item);
            if (transitionSelectBoxDisturb != null) {
                selectBox.setChecked(transitionSelectBoxDisturb.evaluate(item));
            }
            selectBoxList.add(selectBox);
        });
        return selectBoxList;
    }

    /**
     * item转selectBox选择状态干扰器
     */
    public interface TransitionSelectBoxDisturb<E> {
        boolean evaluate(E item);
    }
}
